<?php

use yii\db\Migration;

/**
 * Handles the creation of table `{{%system_group}}`.
 */
class m230831_092408_create_system_group_table extends Migration
{
    const TABLE_NAME = '{{%system_group}}';

    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci ENGINE=InnoDB COMMENT="系统用户组表"';
        }

        $this->createTable(self::TABLE_NAME, [
            'id' => $this->primaryKey(),
            'pid' => $this->integer()->notNull()->defaultValue(0)->comment('父ID'),
            'path' => $this->string()->notNull()->defaultValue('')->comment('路径, 例: -0-1-'),
            'name' => $this->string(50)->notNull()->unique()->comment('管理组名称'),
            'desc' => $this->string()->notNull()->defaultValue('')->comment('描述'),
            'status' => $this->boolean()->notNull()->defaultValue(1)->comment('状态, 0:禁用 1:正常'),
            'assigned' => $this->boolean()->notNull()->defaultValue(0)->comment('已分配权限, 0:否 1:是'),
            'created_at' => $this->dateTime()->notNull(),
            'updated_at' => $this->dateTime()->defaultValue(null),
        ], $tableOptions);

        $this->createIndex('idx_pid', self::TABLE_NAME, ['pid']);
        $this->createIndex('idx_status', self::TABLE_NAME, ['status']);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable(self::TABLE_NAME);
    }
}
